package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import pojo.News;
import pojo.User;

import service.NewsService;
import utils.APage;

@Controller
@RequestMapping("/news")
public class NewsController {
	
	Logger log = Logger.getLogger(NewsController.class); 

	@Resource
	private NewsService newsService;

	@RequestMapping(value = "/goNewsItems.html")
	public String goNewsItems(
			Map<String, Object> map,
			// 当前页,请求页
			@RequestParam(value = "cp", required = false, defaultValue = "1") int currentPage,
			// 前端
			@RequestParam(value = "unit", required = false) String unit,
			@RequestParam(value = "title", required = false) String title,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime
		) {
		//设置每页条数
		int pageSize = 5;
		//获取总集合
		List<News> list = newsService.selectNewsList(unit, title, startTime, endTime,null);
		//获取总页数
		int pageCount = newsService.getPageCount(list.size(), pageSize);
		//最小页
		if (currentPage < 1) {
			currentPage = 1;
		}
		//最大页
		if (currentPage > pageCount){
			currentPage = pageCount;
		}
		List<News> nl = APage.collSetPagingPojo(currentPage, pageSize, list);
		map.put("nl", nl);
		map.put("pc", pageCount);
		map.put("cp", currentPage);
		map.put("unit", unit);
		map.put("title", title);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return "NewsItems";
	}
	
	@RequestMapping(value="handleNews")
	@ResponseBody
	public String handleNews(
			News news,
			HttpSession session,
			@RequestParam(value="ids[]",required=false)List<Integer> ids,
			/*HttpServletRequest request,*/
			@RequestParam(value="type")String type
		){
		Map<String,Object> out = new HashMap<String,Object>();
		log.debug("=========================================>"+news.toString());
		if(type.equals("add")){
			User user = (User)session.getAttribute("u");
			news.setuId(user.getId());
			int result = newsService.add(news);
			if(result>-1){
				out.put("success", "成功");
			}else{
				out.put("success", "失败");
			}
		}
		if(type.equals("update")){
			int result = newsService.update(news);
			if(result>-1){
				out.put("success", "成功");
			}else{
				out.put("success", "失败");
			}
		}
		if(type.equals("delect")){
			if(ids!=null){
				System.out.println(ids.toString());
				int result = newsService.delete(ids);
				if(result == ids.size()){
					out.put("success", "成功");					
				}else{
					out.put("success", "异常");
				}
			}
			if(news.getId()!=null){
				ids = new ArrayList<Integer>();
				ids.add(news.getId());
				int result = newsService.delete(ids);
				if(result == ids.size()){
					out.put("success", "成功");					
				}else{
					out.put("success", "异常");
				}
			}
			/*System.out.println(request.getParameter("ids"));  
			//注意这里 HttpServletRequest默认加了 []  不管 js中加不 加 []   
			System.out.println(request.getParameterValues("ids[]").length);  
	        System.out.println(Arrays.toString(request.getParameterValues("ids[]")));  
	        System.out.println(request.getParameterValues("ids[]").getClass());  */
		}
		return JSON.toJSONString(out); 
	}
}
